<?php
/**

 */
class Form_Account extends App_Form
{
    public function __construct($options = null)
    {
        parent::__construct($options);

        // change the class on the outer <dl> tag
        $this->getDecorator('HtmlTag')->setOption('class', 'form');

        $this->setName('account');

        $email = $this->textElement('email', 'Email');
        
        $check = $this->checkBoxElement('change_pwd', 'change password');
        
        $password = $this->password('password','Password');
        
        $password2 = $this->password('password2','Password Again');

        $items = App_Timezone::getTimezones();
        $timezone = $this->selectElement('timezone','Timezone',$items );    

        $submit = $this->submit('submitbutton', 'Save');

        // create array of elements to add to the form
        $elements = array($email, $timezone, $check, $password, $password2, $submit);

        $this->addElements($elements);

        // add error summary decorator (will list all validation errors at the
        // top of the form - all 'Error' decorators should be disabled since we
        // are not showing the errors next to the input item (just turning the
        // labels red)
        $this->addDecorator(new App_Form_Decorator_FormErrors(
                array('placement'=>Zend_Form_Decorator_Abstract::PREPEND,
                    'message'=>'Error')));

    }


    public function isValid($data)
    {
        // if checkbox is not set to change password, remove required flags
        if (!isset($data['change_pwd']) || (int)$data['change_pwd'] == 0){
            $this->getElement('password')->setRequired(false);
            $this->getElement('password2')->setRequired(false);
        }
        else{
            // inject another validator on the password element to check
            // password1/password2 equality
            $password = $this->getElement('password');
            $password2= $this->getElement('password2');
            $password->addValidator(new App_Validate_PasswordMatch($data['password2']));
        }

        return parent::isValid($data);
    }


}
